package com.qq.lht.androidgame24;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/9/25 0025.
 * 通过bullet提取的sprite父类，利于代码复用
 */

class Sprite {
    private ArrayList<Bitmap> bitmaps;
    private Bitmap bitmap;
    private int width;
    private int height;
    private int x;
    private int y;
    //2018-9-28 09:20  去除RPG游戏不需要的移动速度
    private int speedX;
    private int speedY;
    private boolean isVisible;
    //2018年9月26日09:39:23添加多帧动画
    private int     frameNumber;
    private int[]   frameX;
    private int[]   frameY;
    private Rect    src;
    private Rect    dst;
    //2018-9-28 09:22 修改frameIndex为frameSequenceIndex（当前帧索引）
    //private int frameSequenceIndex;
    private int     frameSequenceIndex;
    //2018-9-28 09:24 添加帧序列
    private int[]   frameSequence;

    public Sprite(ArrayList<Bitmap> bitmaps,int width, int height) {
        this.bitmaps = bitmaps;
        this.width = width;
        this.height = height;
        frameSequence = new int[bitmaps.size()];
        for (int i = 0; i < frameSequence.length; i++) {
            frameSequence[i] = i;
        }
    }

    //2018-09-26 09:41:20修改构造方法
    public Sprite(Bitmap bitmap) {
        this(bitmap,bitmap.getWidth(),bitmap.getHeight());
    }

    //2018年9月26日09:42:20添加重写构造方法
    public Sprite(Bitmap bitmap, int width, int height) {
        this.bitmap = bitmap;
        this.width = width;
        this.height = height;
        int w = bitmap.getWidth()/width;//大图分割为小图
        int h = bitmap.getHeight()/height;
        frameNumber = w * h;//总帧数
        frameX = new int[frameNumber];//存放每一帧坐标
        frameY = new int[frameNumber];
        for (int i = 0; i < h; i++) {//行
            for (int j = 0; j < w; j++) {//列
                frameX[w*i+j] = width * j;
                frameY[w*i+j] = height * i;
            }
        }
        src = new Rect();
        dst = new Rect();
        //2018-9-28 09:26 添加初始化默认帧序列
        frameSequence = new int[frameNumber];
        for (int i = 0; i < frameNumber; i++) {
            frameSequence[i] = i;
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getSpeedX() {
        return speedX;
    }

    public void setSpeedX(int speedX) {
        this.speedX = speedX;
    }

    public int getSpeedY() {
        return speedY;
    }

    public void setSpeedY(int speedY) {
        this.speedY = speedY;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public boolean isVisible() {
        return isVisible;
    }

    public void setVisible(boolean visible) {
        isVisible = visible;
    }

    public void setPosition(int x, int y){
        this.x = x;
        this.y = y;
    }

    //2018年9月26日09:47:40添加frameIndex、frameNumber的get/set方法
    public int getFrameNumber() {
        return frameNumber;
    }

    public void setFrameNumber(int frameNumber) {
        this.frameNumber = frameNumber;
    }

    public int getFrameSequenceIndex() {
        return frameSequenceIndex;
    }

    public void setFrameSequenceIndex(int frameSequenceIndex) {
        this.frameSequenceIndex = frameSequenceIndex;
    }

    //2018-9-28 09:27 添加frameSequence的get/set方法
    public int[] getFrameSequence() {
        return frameSequence;
    }

    public void setFrameSequence(int[] frameSequence) {
        this.frameSequence = frameSequence;
    }

    public void logic(){
        //2018-9-28 09:20  去除RPG游戏不需要的移动速度
        move(speedX,speedY);
        outOfBounds();
    }

    public void move(float distanceX, float distanceY) {
        x += distanceX;
        y += distanceY;
        outOfBounds();
    }

    public void draw(Canvas canvas){
        if (isVisible){
            if (bitmap != null) {
                src.set(frameX[frameSequence[frameSequenceIndex]],
                        frameY[frameSequence[frameSequenceIndex]],
                        frameX[frameSequence[frameSequenceIndex]] + width,
                        frameY[frameSequence[frameSequenceIndex]] + height);
                dst.set(x, y, x + width, y + height);
                canvas.drawBitmap(bitmap, src, dst, null);
            }else if (bitmaps != null){
                canvas.drawBitmap(bitmaps.get(frameSequence[frameSequenceIndex]),x,y,null);
            }
        }
    }

    public void outOfBounds() {
        if (getX() < 0 || getX() > 1280 || getY() < 0 || getY() > 768){
            setVisible(false);
        }
    }

    //2018年9月26日09:51:29添加碰撞检测方法（排除法）
//    public boolean collisionWith(Sprite sprite){
//        if (!isVisible() || !sprite.isVisible()){//排除隐藏
//            return false;
//        }
//        if (getX() < sprite.getX() && getX()+getWidth() < sprite.getX()) {//排除右侧
//            return false;
//        }
//        if (sprite.getX() < getX() && sprite.getX()+sprite.getWidth() < getX()){//排除左侧
//            return false;
//        }
//        if (getY() < sprite.getY() && getY()+getHeight() < sprite.getY()){//排除下侧
//            return false;
//        }
//        if (sprite.getY() < getX() && sprite.getY()+sprite.getHeight() < getY()){//排除上侧
//            return false;
//        }
//        if (getY() > sprite.getY()+getWidth()/2 || sprite.getY()-getWidth()/2 < getY()){//排除不在同一列上
//            return false;
//        }
//        return true;
//    }
    public boolean collisionWith(Sprite sprite) {//碰撞检测，排除法
        if (!isVisible() || !sprite.isVisible()) {//排除隐藏
            return false;
        }
        if (getX() < sprite.getX() && getX() + getWidth() < sprite.getX()) {//排除右侧
            return false;
        }
        if (sprite.getX() < getX() && sprite.getX() + sprite.getWidth() < getX()) {//排除左侧
            return false;
        }
        if (getY() < sprite.getY() && getY() + getHeight() < sprite.getY()) {//排除下侧
            return false;
        }
        if (sprite.getY() < getY() && sprite.getY() + sprite.getHeight() < getY()) {
            return false;
        }
        return true;
    }


    //2018年9月26日09:57:19t添加下一帧方法
    public void nextFrame(){
        //2018-9-28 09:35 修改获取下一帧的方法，适用于多行多列帧动画
        //setFrameIndex((getFrameSequenceIndex()+1)%getFrameNumber());
        setFrameSequenceIndex((getFrameSequenceIndex()+1)%getFrameSequence().length);
    }
}
